package Indexserver.Utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;


public class Utils{
	public static String getSummary(String dID) {
		BufferedReader inbuffer = null;
		sIDpID sidpid = documentID.toSIDPID(Long.valueOf(dID));
		String fname = Squid.prepare(Constant.CORPUS, sidpid.sID, sidpid.pID)
				+ ".txt";
		String res = "";
		ArrayList<String> arr = new ArrayList<String>();
		try {
			inbuffer = new BufferedReader(new InputStreamReader(
					new FileInputStream(fname), "UTF-8"));
			inbuffer.readLine();
			inbuffer.readLine();
			String line = "";

			while ((line = inbuffer.readLine()) != null) {
				if (!line.trim().equals("")) {
					arr.add(line);
					if (arr.size() > 5)
						break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (inbuffer != null)
				try {
					inbuffer.close();
				} catch (Exception e) {
				}
		}
		try {
			int i=0;
			for(i=0; i < arr.size();i++){
				res+=arr.get(i)+"<br>";
				if(res.length()>130) break;
			}
			String []a = res.split(" ");
			res="";
			for(i=0; i < Math.min(80,a.length);i++){
				if(a[i].length()<25)
					res += a[i]+" ";
				else
					res += a[i].substring(0,25)+"..."+" ";
			}
			if(a.length>80) res+="...";
			
		} catch (Exception e) {
		}
		arr.clear();
		return res.replace("TIN LI\u00caN QUAN", "").replace("Tin li\u00ean quan", "");
	}
}